package com.wanxi.newstype.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.wanxi.common.entity.NewsTypeModel;
import com.wanxi.common.result.ResultDTO;
import com.wanxi.common.util.Tool;
import com.wanxi.newstype.mapper.NewsTypeMapper;
import com.wanxi.newstype.service.NewsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NewsTypeServiceImpl implements NewsTypeService {
    @Autowired
    private NewsTypeMapper newsTypeMapper;
    @Override
    public ResultDTO add(NewsTypeModel newsTypeModel) {
        LambdaQueryWrapper<NewsTypeModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(NewsTypeModel::getId)
                .eq(newsTypeModel.getName()!=null,NewsTypeModel::getName,newsTypeModel.getName());
        boolean exists = newsTypeMapper.exists(queryWrapper);
        if(exists){
            return ResultDTO.builder()
                    .code(0)
                    .msg("hava same name")
                    .data("")
                    .count(-1)
                    .build();
        }else {
            return ResultDTO.builder()
                    .code(0)
                    .msg("")
                    .data("")
                    .count(newsTypeMapper.insert(newsTypeModel))
                    .build();
        }

    }

    @Override
    public ResultDTO update(NewsTypeModel newsTypeModel) {
        LambdaQueryWrapper<NewsTypeModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(NewsTypeModel::getId)
                .eq(newsTypeModel.getName()!=null,NewsTypeModel::getName,newsTypeModel.getName());
        boolean exists = newsTypeMapper.exists(queryWrapper);
        if(exists){
            return ResultDTO.builder()
                .code(0)
                .msg("")
                .data("")
                .count(newsTypeMapper.updateById(newsTypeModel))
                .build();

        }else {
            return ResultDTO.builder()
                    .code(0)
                    .msg("hava no")
                    .data("")
                    .count(-1)
                    .build();
        }
    }

    @Override
    public ResultDTO delete(Long id) {
        return ResultDTO.builder()
                .code(0)
                .msg("")
                .data("")
                .count(newsTypeMapper.deleteById(id))
                .build();
    }

    @Override
    public ResultDTO findById(Long id) {
    LambdaQueryWrapper<NewsTypeModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(NewsTypeModel::getId,NewsTypeModel::getName,NewsTypeModel::getImgHref,
                        NewsTypeModel::getContent,NewsTypeModel::getEnable)
                .eq(id != null,NewsTypeModel::getId,id);
        NewsTypeModel newsType = newsTypeMapper.selectOne(queryWrapper);
        return ResultDTO.builder()
                .code(0)
                .msg("")
                .data(newsType)
                .build();
    }

    @Override
    public ResultDTO findAll(NewsTypeModel newsTypeModel) {
        newsTypeModel.setPage(Tool.nullToPage(newsTypeModel.getPage()));
        newsTypeModel.setLimit(Tool.nullToLimit(newsTypeModel.getLimit()));

        LambdaQueryWrapper<NewsTypeModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(NewsTypeModel::getId,NewsTypeModel::getName,NewsTypeModel::getImgHref,
                        NewsTypeModel::getContent,NewsTypeModel::getEnable)
                .like(newsTypeModel.getName()!=null && newsTypeModel.getName()!="",NewsTypeModel::getName,newsTypeModel.getName())
                .like(newsTypeModel.getContent()!=null&& newsTypeModel.getContent()!="",NewsTypeModel::getContent,newsTypeModel.getContent())
                .eq(newsTypeModel.getEnable()!=null&& newsTypeModel.getEnable()!=-1,NewsTypeModel::getEnable,newsTypeModel.getEnable());


        IPage<NewsTypeModel> page = new Page<>(newsTypeModel.getPage(),newsTypeModel.getLimit());
        IPage<NewsTypeModel> result = newsTypeMapper.selectPage(page, queryWrapper);

        return ResultDTO.builder()
                .code(0)
                .msg("")
                .count((int) result.getTotal())
                .data(result.getRecords())
                .build();
    }

    @Override
    public NewsTypeModel findNewsTypeById(Long id) {
        LambdaQueryWrapper<NewsTypeModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(NewsTypeModel::getId,NewsTypeModel::getName,NewsTypeModel::getImgHref,
                        NewsTypeModel::getContent,NewsTypeModel::getEnable)
                .eq(id != null,NewsTypeModel::getId,id);
        NewsTypeModel newsType = newsTypeMapper.selectOne(queryWrapper);
        return newsType;
    }
}
